package edu.guigu.serverservice.local;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;


// 绑定服务(本地), 通过bindService()启动
public class BindService extends Service {

    private MyBinder myBinder;

    public BindService() {
    }

    // 首次创建服务时，系统将调用此方法来执行一次性设置程序
    // 如果服务已在运行，则不会调用此方法。该方法只被调用一次
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("TAG", getClass().getSimpleName() + " onCreate: ");
    }

    // 绑定服务时(bindService)才会调用
    @Override
    public IBinder onBind(Intent intent) {
        Log.d("TAG", getClass().getSimpleName() + " onBind: ");
        if (myBinder == null) {
            myBinder = new MyBinder();
        }
        return myBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("TAG", getClass().getSimpleName() + " onUnbind ");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("TAG", getClass().getSimpleName() + " onDestroy ");
        myBinder = null;
    }

    public class MyBinder extends Binder {

        public BindService getService() {
            return BindService.this;
        }
    }

    public String getMessage(){
        return "hello,我是BindService里的数据";
    }

}